package com.meida.module.bus.provider.handler;


import com.egzosn.pay.common.api.PayService;
import com.egzosn.pay.common.bean.PayMessage;
import com.meida.common.base.entity.EntityMap;
import com.meida.common.base.handler.PayInfoHandler;
import com.meida.common.base.utils.FlymeUtils;
import com.meida.common.enums.OrderStatusEnum;
import com.meida.common.mybatis.model.ResultBody;
import com.meida.common.mybatis.query.CriteriaSave;
import com.meida.common.security.OpenHelper;
import com.meida.common.utils.ApiAssert;
import com.meida.common.utils.JsonUtils;
import com.meida.module.bus.client.entity.VipType;
import com.meida.module.bus.client.entity.VipUser;
import com.meida.module.bus.provider.service.BusOrderInfoService;
import com.meida.module.bus.provider.service.BusVipTypeService;
import com.meida.module.bus.provider.service.VipTypeService;
import com.meida.module.pay.client.entity.PayInfo;
import com.meida.module.pay.provider.handler.PaySuccessHandler;
import com.meida.module.pay.provider.service.PayInfoService;
import com.meida.module.user.provider.service.AppUserService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 会员购买处理器
 *
 * @author zyf
 */
@Component("busVipOrderPayHandler")
@Log4j2
public class VipOrderPayHandler implements PayInfoHandler, PaySuccessHandler {

    @Autowired
    private PayInfoService payInfoService;
    @Resource
    BusVipTypeService busVipTypeService;

    @Autowired
    private AppUserService userService;

    @Resource
    private BusOrderInfoService busOrderInfoService;

    @Autowired
    private VipTypeService vipTypeService;

    @Override
    public ResultBody validate(CriteriaSave cs, EntityMap requestMap) {
        Long userId = OpenHelper.getUserId();
        Long vipTypeId = requestMap.getLong("vipTypeId");
        VipType vipType = vipTypeService.getById(vipTypeId);
        if (FlymeUtils.isEmpty(vipType)) {
            return ResultBody.failed("会员类型不存在");
        } else {
            requestMap.put("orderAmount", vipType.getPrePrice());
        }
        //PayInfo ordOrder = payInfoService.findNoPayOrder(VipUser.class, userId);
        //if (FlymeUtils.isNotEmpty(ordOrder)) {
        //    return ResultBody.failed("您有未支付的订单");
        //}

        return ResultBody.ok();
    }

    @Override
    public void prepare(CriteriaSave cs, EntityMap params) {
    }

    @Override
    public void complete(CriteriaSave cs, EntityMap params, Object object) {

    }

    /**
     * 创建订单
     *
     * @param cs         对象模型
     * @param requestMap 参数接收
     * @return
     */
    @Override
    public Object getPayInfo(CriteriaSave cs, EntityMap requestMap, String outOrderNo) {
        PayInfo payInfo = cs.getEntity(PayInfo.class);
        Long vipTypeId = requestMap.getLong("vipTypeId");
        VipType vipType = vipTypeService.getById(vipTypeId);
        ApiAssert.isNotEmpty("会员不存在", vipType);
        payInfo.setOrderTitle("会员购买");
        payInfo.setCompanyId(requestMap.getLong("userId"));
        payInfo.setOrderEntity(VipUser.class.getSimpleName());
        payInfo.setOrderBody(vipType.getVipName());
        payInfo.setOrderAmount(vipType.getPrePrice());
        payInfo.setBusId(vipType.getVipTypeId());
        return payInfo;
    }

    /**
     * 订单回调检验
     *
     * @param payMessage 支付回调消息
     * @param context    上下文参数,可传递到success方法内
     * @param payService 支付服务
     * @param outTradeNo 订单号
     * @return
     */
    @Override
    public boolean validate(PayMessage payMessage, Map<String, Object> context, PayService payService, String outTradeNo) {
        System.out.println("支付回调" + outTradeNo);
        return payInfoService.validate(outTradeNo);
    }


    /**
     * 订单回调业务处理
     *
     * @param payMessage 支付回调消息
     * @param outTradeNo 订单号
     * @param context    上下文参数
     * @param payService 支付服务
     * @return
     */
    @Override
    public boolean success(PayMessage payMessage, String outTradeNo, Map<String, Object> context, PayService payService) {
        //获取创建订单时参数
        log.info("支付回调{}----{},", JsonUtils.beanToJson(payMessage), JsonUtils.beanToJson(context));


        EntityMap params = payInfoService.getOrderParams(outTradeNo);
        Long vipTypeId = params.getLong("vipTypeId");

        Long userId = params.getLong("userId");
        //vipUserService.setUserVip(userId, vipTypeId);
        busVipTypeService.buyVip(vipTypeId, userId);
        busOrderInfoService.saveVipOrder(vipTypeId, userId, outTradeNo, payMessage.getPayMessage().get("transaction_id"));
        context.put("transactionType", payMessage.getPayMessage().get("transaction_id"));
        return payInfoService.success(context, OrderStatusEnum.FINISHED.getCode());
    }

}
